package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.Result;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.UserVideo;
import com.example.demo.service.UserVideoService;
import com.example.demo.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.util.calendar.CalendarDate;

import javax.servlet.http.HttpServletResponse;
import java.io.Console;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 第五小组
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/bilibili")
public class UserVideoController {

    @Autowired
    UserVideoService userVideoService;

    @GetMapping("/user_video")
    public Result user_video(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        return Result.succ(userVideoService.listMaps());
    }

    //登上排行榜次数及最高排名统计
    @GetMapping("/uploader/video_count")
    public Result video_count(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        List<Map<String, Object>> userVideoList = userVideoService.listMaps();
        int rankCount = 0;
        long highestRank = Util.LOWEST_RANK;
        for (Map<String, Object> i : userVideoList) {
            long rank = (long)i.get("history_rank");
            if(rank>0){
                rankCount++;
                if (rank<highestRank){
                    highestRank = rank;
                }
            }
        }
        highestRank=highestRank==Util.LOWEST_RANK?0:highestRank;
        List<Integer> rac = new ArrayList<>();
        rac.add(rankCount);
        rac.add((int)highestRank);
        return Result.succ(rac);
    }

    //投稿时间分布
    @GetMapping("/uploader/time_count")
    public Result time_count(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String, Object>> userVideoList = userVideoService.listMaps();
        Date date;
        Calendar calendar = Calendar.getInstance();
        //Calendar currentCalendar = Calendar.getInstance();
        int[] timesCount = new int[24];
        for (Map<String, Object> i : userVideoList) {
            try {
                date = simpleDateFormat.parse(i.get("pubdate").toString());
                calendar.setTime(date);
                //该时段投稿数加一
                timesCount[calendar.get(Calendar.HOUR_OF_DAY)]++;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return Result.succ(timesCount);
    }

//    投稿时间与播放量之间的关系
    @GetMapping("/uploader/time_view")
    public Result time_view(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String, Object>> userVideoList = userVideoService.listMaps();
        Date date;
        Calendar calendar = Calendar.getInstance();
        int[] timesCount = new int[24];
        long[] viewsCount = new long[24];
        long views;
        int times;
        for (Map<String, Object> i : userVideoList) {
            try {
                date = simpleDateFormat.parse(i.get("pubdate").toString());
                calendar.setTime(date);
                views = Long.parseLong(i.get("view").toString());
                times = calendar.get(Calendar.HOUR_OF_DAY);
                timesCount[times]++;
                viewsCount[times] += views;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //各时段总播放量数组与各时段投稿数做除法
        for (int i = 0; i < viewsCount.length; i++) {
            if(timesCount[i] > 0){
                viewsCount[i] /= timesCount[i];
            }
        }
        return Result.succ(viewsCount);
    }

    @GetMapping("/uploader/duration_count")
    public Result duration_count(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        List<Map<String, Object>> userVideoList = userVideoService.listMaps();
        int[] timesCount = new int[5];
        long duration;
        for (Map<String, Object> i : userVideoList) {
            duration = (long)i.get("duration");
            if(duration<301){
                timesCount[0]++;
            } else if (duration<901){
                timesCount[1]++;
            } else if (duration<1801){
                timesCount[2]++;
            } else if (duration<3600){
                timesCount[3]++;
            } else{
                timesCount[4]++;
            }
        }
        return Result.succ(timesCount);
    }

//    视频时长分布与播放量等视频数据之间的关系
    @GetMapping("/uploader/duration_view")
    public Result duration_view(@RequestParam("uid") String uid, HttpServletResponse response){
        Util.exec("\\脚本\\user_video.py ",uid);
        List<Map<String, Object>> userVideoList = userVideoService.listMaps();
        int[] timesCount = new int[5];
        long[] viewsCount = new long[5];
        long duration;
        long views;
        for (Map<String, Object> i : userVideoList) {
            duration = (long)i.get("duration");
            views = Long.parseLong(i.get("view").toString());
            if(duration<301){
                timesCount[0]++;
                viewsCount[0] += views;
            } else if (duration<901){
                timesCount[1]++;
                viewsCount[1] += views;
            } else if (duration<1801){
                timesCount[2]++;
                viewsCount[2] += views;
            } else if (duration<3600){
                timesCount[3]++;
                viewsCount[3] += views;
            } else{
                timesCount[4]++;
                viewsCount[4] += views;
            }
        }
        for (int i = 0; i < viewsCount.length; i++) {
            if(timesCount[i] > 0){
                viewsCount[i] /= timesCount[i];
            }
        }
        return Result.succ(viewsCount);
    }

}
